package x;

import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.JOptionPane;

public class Entity {
        // x and y in terms of the grid
        public float x = 0;
        public float y = 0;
        /** The image to draw for this entity */
        public Image image;

        private Map map;
        /** The angle to draw this entity at */
        private float ang;
        /** The size of this entity, this is used to calculate collisions with walls */
        private float size = 0.3f;

        //this variable is flagged if player is at a door
        public boolean checkDoor = false;
        public int location;


       //these variables are for returning the location of the player
        public float ex = 0;
        public float ey = 0;

        public boolean moveLeft = false;
        public boolean moveRight = false;
        public boolean moveUp = false;
        public boolean moveDown = false;
        public int area;
        public int Id;
        public String Name;

 

        public Entity(Image image, Map map, float x, float y) {
                this.image = image;
                this.map = map;
                this.x = x;
                this.y = y;
                this.Name = "player";
                this.area = 0;
        }
        public Entity() {
                this.moveDown = false;
                this.moveUp = false;
                this.moveLeft = false;
                this.moveRight = false;
                this.image = Game.spriteFront[0];
                this.map = Game.map;
                this.x = 30;
                this.y = 15;
                this.Name = "player";
                this.area = 0;
        }
        public Entity(Image image, Map map, float x, float y, String name){
            this.image=image;
            this.map = map;
            this.x = x;
            this.y = y;
            this.Name = name;
            this.moveDown = false;
            this.moveUp = false;
            this.moveLeft = false;
            this.moveRight = false;
            this.area = 0;
        }
        public Entity(Image image, Map map, float x, float y, int zone) {
                this.image = image;
                this.map = map;
                this.x = x;
                this.y = y;
                this.Name = "player";
                this.area = zone;
        }

        public Entity(Image image, Map map, float x, float y, String name, int zone){
            this.image=image;
            this.map = map;
            this.x = x;
            this.y = y;
            this.Name = name;
            this.moveDown = false;
            this.moveUp = false;
            this.moveLeft = false;
            this.moveRight = false;
            this.area = zone;
        }



        /**
         * Move this entity a given amount. This may or may not succeed depending
         * on collisions
         *
         * @param dx The amount to move on the x axis
         * @param dy The amount to move on the y axis
         * @return True if the move succeeded
         */
        public boolean move(float dx, float dy) {
            //if an event has happened, then stop the rest of this function from running
           if (checkDoor == true)
           return false;
           if(dx>0 &&dy>0)
           {
               moveUp = false;
               moveDown = true;
               moveLeft = false;
               moveRight = false;
           }
           if(dx<0 &&dy>0)
           {
               moveUp = false;
               moveDown = true;
               moveLeft = false;
               moveRight = false;
           }
           if(dy>0)
           {
               moveUp = false;
               moveDown = true;
               moveLeft = false;
               moveRight = false;
           }
           if(dy<0 || (dy<0 && dx>0) || (dy<0 && dx<0))
           {
                moveUp = true;
                moveDown = false;
                moveLeft = false;
                moveRight = false;
           }
           if(dx>0 && dy==0)
           {
                moveUp = false;
                moveDown = false;
                moveLeft = false;
                moveRight = true;
           }
           if(dx<0 && dy==0)
           {
                moveUp = false;
                moveDown = false;
                moveLeft = true;
                moveRight = false;
           }

                // work out what the new position of this entity will be
                float nx = x + dx;
                float ny = y + dy;

        //set the static variables
        ex = nx;
        ey = ny;



                // check if the new position of the entity collides with
                // anything
                if (validLocation(nx, ny)) {
                        // if it doesn't then change our position to the new position
                        x = nx;
                        y = ny;

                        // and calculate the angle we're facing based on our last move
                        ang = (float) (Math.atan2(dy, dx) - (Math.PI / 2));
                        return true;
                }

                // if it wasn't a valid move don't do anything apart from
                // tell the caller
                return false;
        }

        /**
         * Check if the entity would be at a valid location if its position
         * was as specified
         *
         * @param nx The potential x position for the entity
         * @param ny The potential y position for the entity
         * @return True if the new position specified would be valid
         */
        public boolean validLocation(float nx, float ny) {
                // here we're going to check some points at the corners of
                // the player to see whether we're at an invalid location
                // if any of them are blocked then the location specified
                // isn't valid
                if (map.blocked(nx - size, ny - size)) {
                        return false;
                }
                if (map.blocked(nx + size, ny - size)) {
                        return false;
                }
                if (map.blocked(nx - size, ny + size)) {
                        return false;
                }
                if (map.blocked(nx + size, ny + size)) {
                        return false;
                }

                //***********************************
                if (map.buildingcomplete(nx - size, ny - size)) {
                        return false;
                }
                if (map.buildingcomplete(nx + size, ny - size)) {
                        return false;
                }
                if (map.buildingcomplete(nx - size, ny + size)) {
                        return false;
                }
                if (map.buildingcomplete(nx + size, ny + size)) {
                        return false;
                }

                //**************************************
                /*if (map.OUTERBLOCKED(nx - size, ny - size)) {
                        return false;
                }
                if (map.OUTERBLOCKED(nx + size, ny - size)) {
                        return false;
                }
                if (map.OUTERBLOCKED(nx - size, ny + size)) {
                        return false;
                }
                if (map.OUTERBLOCKED(nx + size, ny + size)) {
                        return false;
                }
*/

                        //check if a door has been opened
                      checkDoor = buildingHandler(nx, ny);


                // if all the points checked are unblocked then we're in an ok
                // location
                return true;
        }

        /**
         * Draw this entity to the graphics context provided.
         *
         * @param g The graphics context to which the entity should be drawn
         */
        public void paint(Graphics2D g) {
                // work out the screen position of the entity based on the
                // x/y position and the size that tiles are being rendered at. So
                // if we're at 1.5,1.5 and the tile size is 10 we'd render on screen
                // at 15,15.
                int xp = (int) (Map.TILE_SIZE * x);
                int yp = (int) (Map.TILE_SIZE * y);

                // rotate the sprite based on the current angle and then
                // draw it


                g.drawImage(image, (int) (xp - 16), (int) (yp - 16), null);

        }




        //this function is to check if the entity is nearing a certain tile (door)
        public boolean buildingHandler(float nx, float ny){
            //calls building function and receives the location the user is in
            //returns -1 otherwise
            //System.out.println("buid handler being run");
            location = map.buildingChecker(nx, ny);

            if (location == 1){


           return true;}

            if (location == 2){

            return true;}
            if (location == 3){

           return true;}

            if (location == 4){

            return true;}



return false;
        }


            //return x position
        public float returnx(){
            return ex;}

        //return y position
        public float returny(){
            return ey;}

}
